package be.nepherte.movmanager.client.ui.components;

import java.util.ArrayList;
import java.util.List;

/**
 * This class represents one row in a sortable table.
 * 
 * <p>
 * Copyright © 2010 Bart Verhoeven
 * </p>
 * 
 * <p>
 * This file is part of MovManager.
 * </p>
 * 
 * <p>
 * MovManager is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * </p>
 * 
 * <p>
 * MovManager is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * </p>
 * 
 * <p>
 * You should have received a copy of the GNU General Public License along with
 * MovManager. If not, see <a
 * href="http://www.gnu.org/licenses/">http://www.gnu.org/licenses</a>.
 * </p>
 * 
 * @author	Bart Verhoeven, Parvinder Thapar
 * @version	0.1
 */
@SuppressWarnings("rawtypes")
public class RowData implements Comparable {
	/**The list of the column values in this row.*/
	List<Comparable<?>> columnValues = new ArrayList<Comparable<?>>();
	/**The current sorted column index.*/
	int sortColIndex = 0;

	/**
	 * Adds the comparable value to the list of column values of this row.
	 * The value will be added to the end of the column value list.
	 * 
	 * @param 	value
	 * 			the comparable item you want to add
	 */
	public void addColumnValue(Comparable<?> value){
		this.columnValues.add(value);
	}
	
	/**
	 * Adds the comparable value to the list of column values of this row at the
	 * specified index. Any previous value at that column will be overwritten
	 * with the given value.  When the given index is greater than the current
	 * number of columns in this row, this row will be appended with extra
	 * null columns till the specified index.
	 * 
	 * @param	index
	 *          the index of the column where the value should be put in
	 * @param 	value
	 *          the comparable value you want to add
	 */
	public void addColumnValue(int index, Comparable<?> value){
		if (index >= this.columnValues.size())
			addNullColumns(index);
		
		this.columnValues.set(index, value);
	}	

	/**
	 * Retrieves the comparable object from the column values of this row.
	 * 
	 * @param	index
	 * 			the column index of the object you want to retrieve
	 * @return 	the object at the specified column index
	 */
	public Object getColumnValue(int index){
		return this.columnValues.get(index);
	}	
	
	/**
	 * Retrieves the list of column values of this row.
	 * 
	 * @return	column values
	 */
	public List<Comparable<?>> getColumnValues() {
		return columnValues;
	}

	/**
	 * Sets the list of comparable column values of this row.
	 * 
	 * @param	columnValues
	 * 			the list of comparable column values
	 */
	public void setColumnValues(List<Comparable<?>> columnValues) {
		this.columnValues = columnValues;
	}

	/**
	 * Retrieves the current column index being sorted.
	 * 
	 * @return 	sortColIndex
	 */
	public int getSortColIndex() {
		return sortColIndex;
	}

	/**
	 * Sets the current sorted column index.
	 * 
	 * @param	sortColIndex
	 * 			the current sorted column index
	 */
	public void setSortColIndex(int sortColIndex) {
		this.sortColIndex = sortColIndex;
	}

	/**
	 * Compares a column value of this row with the value in the same column of another row.
	 * 
	 * @param	other
	 * 			the other row that will be compared with this row
	 */
	@Override
	public int compareTo(Object other) {
		if (null == other || !(other instanceof RowData))
			return -1;
		
		RowData otherRow = (RowData) other;
		@SuppressWarnings("unchecked")
		Comparable<Comparable<?>> obj1 = (Comparable<Comparable<?>>)this.getColumnValue(this.sortColIndex);
		Comparable<?> obj2 = (Comparable<?>)otherRow.getColumnValue(this.sortColIndex);
		return obj1.compareTo(obj2);
	}
	
	/**
	 * Appends extra columns to the end of the existing columns till the given index.  The columns are
	 * filled with null objects. If index is already an existing column, no columns will be added.
	 *  
	 * @param	index
	 * 			the index of the last extra column that has to be added and filled with null objects
	 */
	private void addNullColumns(int index){
		for (int nullIndex=this.columnValues.size(); nullIndex<=index; nullIndex++)
			columnValues.add(null);
	}
}
